package com.androidwwf.jwr;

import java.util.Random;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;

public class GameRandomPictureActivity extends Activity {
		
	private Random rgenerator = new Random();
	private int rnumber = 0;
	private int counter = 0;

	private GridView gridView;
	private TextView textView;
 
	static final String[] IMAGES0 = new String[] { 
		"test1", "test2","test3", "test4" };
	
	static final String[] IMAGES1 = new String[] { 
		"test5", "test6","test7", "test8" };
	
	static final String[] IMAGES2 = new String[] { 
		"test9", "test10","test11", "test12" };
	
	static final String[] IMAGES3 = new String[] { 
		"test13", "tes14","test15", "test16" };
	
	static final String[] IMAGES4 = new String[] { 
		"test17", "test18","test19", "test20" };
	
	private int[] imagesPoints = new int[20];
	
	private int totalPoints = 0;
	
	private boolean[] imagesLoaded = new boolean[5];
	
	public GameRandomPictureActivity()
	{
		imagesPoints[0] = 1;
		imagesPoints[1] = 2;
		imagesPoints[2] = 4;
		imagesPoints[3] = 3;
		imagesPoints[4] = 1;
		imagesPoints[5] = 4;
		imagesPoints[6] = 4;
		imagesPoints[7] = 2;
		imagesPoints[8] = 1;
		imagesPoints[9] = 3;
		imagesPoints[10] = 2;
		imagesPoints[11] = 4;
		imagesPoints[12] = 4;
		imagesPoints[13] = 3;
		imagesPoints[14] = 2;
		imagesPoints[15] = 1;
		imagesPoints[16] = 4;
		imagesPoints[17] = 2;
		imagesPoints[18] = 3;
		imagesPoints[19] = 2;
		
		for(int i = 0; i < imagesLoaded.length; i++)
		{
			imagesLoaded[i] = false;
		}
	}
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gameallpictures);
		
		rnumber = rgenerator.nextInt(2);
		
		textView = (TextView) findViewById(R.id.tvTotal);
		gridView = (GridView) findViewById(R.id.gridview);
		
		if(rnumber == 0)
		{
			gridView.setAdapter(new GameRandomPictureImageAdapter(this, IMAGES0));
			imagesLoaded[0] = true;
		}
		else if(rnumber == 1)
		{
			gridView.setAdapter(new GameRandomPictureImageAdapter(this, IMAGES1));
			imagesLoaded[1] = true;
		}
		else if(rnumber == 2)
		{
			gridView.setAdapter(new GameRandomPictureImageAdapter(this, IMAGES2));
			imagesLoaded[2] = true;
		}
		else if(rnumber == 3)
		{
			gridView.setAdapter(new GameRandomPictureImageAdapter(this, IMAGES3));
			imagesLoaded[3] = true;
		}
		else if(rnumber == 4)
		{
			gridView.setAdapter(new GameRandomPictureImageAdapter(this, IMAGES4));
			imagesLoaded[4] = true;
		}
		
		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				
				
					
					
//				ImageScore entry = new ImageScore(GridViewActivity.this);
//				entry.open();
//				entry.createEntry("test3", 7);
//				entry.close();
//				
//				Intent i = new Intent("android.intent.action.SQLVIEW");
//				startActivity(i);
				
				totalPoints += imagesPoints[position];
				textView.setText(String.valueOf(totalPoints));
				
				if(counter != 4)
				{
					counter++;
				
				rnumber = rgenerator.nextInt(5);
				
				while(true)
				{
					if(imagesLoaded[rnumber] == true)
					{
						rnumber = rgenerator.nextInt(5);
					}
					else
					{
						if(rnumber == 0 && imagesLoaded[0] == false)
						{
							gridView = (GridView) findViewById(R.id.gridview);
							gridView.setAdapter(new GameRandomPictureImageAdapter(GameRandomPictureActivity.this, IMAGES0));
							imagesLoaded[0] = true;
						}
						else if(rnumber == 1 && imagesLoaded[1] == false)
						{
							gridView = (GridView) findViewById(R.id.gridview);
							gridView.setAdapter(new GameRandomPictureImageAdapter(GameRandomPictureActivity.this, IMAGES1));
							imagesLoaded[1] = true;
						}
						else if(rnumber == 2 && imagesLoaded[2] == false)
						{
							gridView = (GridView) findViewById(R.id.gridview);
							gridView.setAdapter(new GameRandomPictureImageAdapter(GameRandomPictureActivity.this, IMAGES2));
							imagesLoaded[2] = true;
						}
						else if(rnumber == 3 && imagesLoaded[3] == false)
						{
							gridView = (GridView) findViewById(R.id.gridview);
							gridView.setAdapter(new GameRandomPictureImageAdapter(GameRandomPictureActivity.this, IMAGES3));
							imagesLoaded[3] = true;
						}
						else if(rnumber == 4 && imagesLoaded[4] == false)
						{
							gridView = (GridView) findViewById(R.id.gridview);
							gridView.setAdapter(new GameRandomPictureImageAdapter(GameRandomPictureActivity.this, IMAGES4));
							imagesLoaded[4] = true;
						}
						break;
					}
				}			
				}else
				{	
					Intent pictureresult = new Intent("com.androidwwf.jwr.GAMERESULT");

					pictureresult.putExtra("typegame", "picture");
					pictureresult.putExtra("points", totalPoints);

					counter = 0;
					totalPoints = 0;
					finish();
					
					startActivity(pictureresult);
				}
			}
		});
 
	}

}
